import router from '@/router'
import { getToken } from '@/utils/auth'

// 导航守卫
router.beforeEach((to, from, next) => {
  const whiteList = ['/login', '', '/', '/home', '/article/']
  const hasToken = getToken() != null

  // 设置标题
  if (to.meta.title) {
    // 判断是否有标题
    document.title = to.meta.title
  }

  if (!hasToken) {
    if (to.path.startsWith('/article/')) {
      next()
      return
    }
    if (whiteList.indexOf(to.path) !== -1) {
      // 说明要去路由是允许的
      next()
    } else {
      next(`/login`) // 没有登
    }
  } else {
    if (to.path === '/') {
      // 已经登陆了 直接跳转到用户首页
      next('/home')
    } else {
      next()
    }
  }
})

router.afterEach(() => {})
